package com.iengoo.spacewar;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief 飞船发射的离子单
 * @email 122162005@qq.com
 * @date 2018-12-26 23:13
 */
public class Plasma {
    private double x;
    private double y;
    private double angle;
    private BufferedImage plasma;
    public static final int SIZE = 92;
    private double speedY = -5; // 子弹飞行的速度为-12像素（子弹向上飞）

    public Plasma(double angle) throws IOException {
        this.angle = angle;
        plasma = ImageIO.read(getClass().getResource("resources/plasma.png"));
        this.x = Config.PANEL_WIDTH / 2 + 1;
        this.y = Config.PANEL_HEIGHT - Craft.SIZE * 5;
    }

    public Plasma(int x, int y, double angle) throws IOException {
        this.angle = angle;
        plasma = ImageIO.read(getClass().getResource("resources/plasma.png"));
        this.x = x;
        this.y = y;
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.rotate(angle, x + (SIZE / 2), y + (SIZE / 2));
        g2d.drawImage(plasma, (int)(x - (SIZE / 2)) , (int)y, SIZE, SIZE, null);
        g2d.rotate(-angle, x + (SIZE / 2), y + (SIZE / 2));
    }

    public void move(Mine mine, Craft craft) {
        this.y += speedY;

        // 计算 x坐标应该移动的速度
        double Sy = mine.getY() - craft.getY();
        double Sx = mine.getX() - craft.getX();
        double tan = Sy / Sx;
        this.x += speedY * Sx / Sy;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}
